<?php
class View
{
	private $_coverFile = null;
	private $_contentFile = null;
	private $_data = array();

	function __construct($cover_file, $content_file) {
		self::setCoverFile($cover_file);
		self::assign('content_file', $content_file);
		self::assign('webroot', ROOT . 'app/webroot/');
		self::assign('common', ROOT . 'app/common/');

		$http = 'http';
		$port = $_SERVER['SERVER_PORT'];
		if($port == 443 ){
			$http = 'https';
		}
		self::assign('http', $http);
	}

	public function write()
	{
		require_once('smarty/Smarty.class.php');
		$smarty = new Smarty();

		$smarty->addTemplateDir( ROOT . 'app/view/');
		$smarty->addTemplateDir( ROOT . 'app/common/');
		$smarty->compile_dir  = ROOT.'app/_compile/';

		$smarty->addPluginsDir(ROOT.'app/view/plugins/');
		//$smarty->default_modifiers = array('string_escape:"html"');
		foreach($this->_data as $k => $v) {
			$smarty->assign($k, $v);
		}

		$smarty->display($this->_coverFile);
	}

	public function setCoverFile($file) {
		$this->_coverFile = $file;
	}

	public function getCoverFile() {
		return $this->_coverFile;
	}

	public function setContentFile($file) {
		$this->_contentFile = $file;
	}

	public function getContentFile() {
		return $this->_contentFile;
	}

	public function assign($k, $v) {
		$this->_data[$k] = $v;
	}

	function render(&$form) {
		//if($form) {
			if(DEVELOPMENT) {
				//self::assign('_form', $form);
				self::assign('debug_file', ROOT . 'app/view/debug/debug_link.tpl');
				self::assign('sql', $_SESSION['sql']);
				unset($_SESSION['sql']);
				self::assign('session', $_SESSION);
		//	}

			// self::assign('form', $form->getMap());
			//self::assign('baseurl', $form->get('__baseurl'));
		}
		self::assign('baseurl', $form->get('baseurl'));
		self::assign('common', $form->get('baseurl').'/app/common');
		self::assign('form', $form->getMap());
		self::write();
	}
}
